package com.bdcom.autotest.ui.snp;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.springframework.util.CollectionUtils;

import com.bdcom.autotest.interaction.socket.DataUtil;
import com.bdcom.autotest.interaction.socket.DevSocket;
import com.bdcom.autotest.interaction.socket.bean.BasePkg;
import com.bdcom.autotest.interaction.step.StepThread;
import com.bdcom.autotest.ui.gif.ImgFactory;

public class SnPanelForCardRxTxTest extends AbsPanel {
	
	String[] testData = new String[] { "SM55011249" , "SM55011250" , "SM55011251" , "SM55011252" , "" , "" , "" , "" , "" , "" , "" }; 
	public  LinkedHashMap<String , JTextField> numToSN = new LinkedHashMap<String, JTextField>();
	public LinkedHashMap<String , JLabel> numToLabel = new LinkedHashMap<String, JLabel>();
	private StepThread mainrun;
	private int slotCount = 0;
	public ArrayList<String> passIndex = new ArrayList();
	private int passCount = 0;
	public SnPanelForCardRxTxTest(StepThread mainrun) {
		this.mainrun = mainrun;
//		this.slotCount = Integer.valueOf(mainrun.getShareVar(mainrun.key_card_cnt));
		this.slotCount = 2;
		this.setBackground(Color.WHITE);
		this.setPreferredSize(new Dimension( 0 , 250 ));
		this.setLayout( new FlowLayout(FlowLayout.LEFT ) );
		Font font2 = new Font("Dialog",Font.PLAIN, 14);
		
		for(   int i=0; i<slotCount  ;i++  ) {
			String indexStr = ""+(i+1);
			JLabel jLabel = new JLabel(indexStr + "槽"+"线卡SN:");
			jLabel.setFont(font2);
			JTextField tsn = new JTextField(18);
			tsn.setFont(font2);
			tsn.setText( testData[i]  );
			byte[] a = DataUtil.int2Bytes(i);
			int b = i;
			JButton jbutton2 = new JButton("开始打流");
			jbutton2.addActionListener( new ActionListener() {
				public void actionPerformed(ActionEvent e){
					if(testCard(b)){
						jbutton2.setEnabled(false);
					}
					
				}
			}
			);
			jbutton2.setFont(font2);
			JLabel jLabel2 = new JLabel("待测试");
			jLabel2.setFont(font2);
			this.add(jLabel);
			this.add(tsn);
			this.add(Box.createHorizontalStrut(10) );
			this.add(jLabel2);
			this.add(jbutton2);
			numToSN.put(indexStr, tsn);
			numToLabel.put(indexStr, jLabel2);
		}
		
	}
	public int getTest(){
		return 1;
	}


	public void setLabelValue(String indexStr,boolean result){
//		JLabel label = numToLabel.get(indexStr);
//		label.setText(result);
		JLabel label = numToLabel.get(indexStr);
		label.show();
		ImageIcon imageIcon = null;
		try {
			if (result) {
				imageIcon = new ImageIcon(ImgFactory.loadIconScale("yes.png",20, 20));
			} else {
				imageIcon = new ImageIcon(ImgFactory.loadIconScale("no.png",20, 20));
			}
			label.setIcon(imageIcon);
			label.setPreferredSize(new Dimension(20, 20));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public boolean testCard(int i){
		String indexStr = ""+(i+1);
		byte[] a = DataUtil.int2Bytes(i+1);
		DevSocket devSocket = mainrun.getDevSokcet();
		byte[] bytes = new byte[6];
		bytes[0] = (byte)0x9E;
		bytes[1] = (byte)0x07;
		bytes[2] = (byte)0x00;
		bytes[3] = (byte)0x02;
		bytes[4] = a[3];
		bytes[5] = (byte)0xAE;
		devSocket.writeMsg(bytes);
		byte cmd = (byte)0x07;
		BasePkg pkg = devSocket.pollAMsg(cmd);
		while(pkg==null){
			pkg = devSocket.pollAMsg(cmd);
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		};
		byte[] data = pkg.getData();
		if(data[1]==(byte)0x00){
			mainrun.addLog(indexStr + "槽线卡端口状态检查成功");
			if(!passIndex.contains(indexStr)){
				passIndex.add(indexStr);
			}
			setLabelValue(indexStr,true);
			return true;
		}else{
			mainrun.addErrorLog(indexStr + "槽线卡端口状态检查失败");
			setLabelValue(indexStr,false);
			return false;
		}
	}
	
	public boolean getTotalResult(){
		boolean result = false;
		while(!result){
			if(passIndex.size() == slotCount){
				result = true;
				return result;
			}
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return result;
	}
}
